<template>
	<view class="login-container">
		<!-- 图标 -->
    <uni-icons class="login-icon" type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!-- 一键登录 -->
    <button class="login-btn" type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
    <!-- 提示文字 -->
    <view class="login-tips">登录享受更多权益~</view>
	</view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
	export default {
		data() {
			return {
				defaultToken:'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
			};
		},
    computed:{
      ...mapState('m_user',['redirectInfo'])
    },
    methods:{
      ...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
      getUserInfo(e) {
        // console.log(e)
        // 用户没有授权
        if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您已取消授权')
        // console.log(e.detail.userInfo)
        this.updateUserInfo(e.detail.userInfo)
        // 获取code并发送登录请求
        this.loginGetToken(e.detail)
      },
      async loginGetToken(info) {
        const [err,res] = await uni.login().catch(err => err)
        // console.log(res)
        if(err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败!')
        // console.log(res.code,info)
        // 声明一个请求对象
        const query = {
          code: res.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }
        const {data} = await uni.$http.post('/api/public/v1/users/wxlogin',query)
        // console.log(data)
        this.updateToken(this.defaultToken)
        this.navigateBack()
      },
      navigateBack() {
        if(this.redirectInfo && this.redirectInfo.openType==='switchTab') {
          uni.switchTab({
            url:this.redirectInfo.from,
            complete:() => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    }
	}
</script>

<style lang="scss">
.login-container {
  height: 750rpx;
  background-color: #F8F8F8;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  // 通过伪元素的方式实现底部圆弧
  &::after {
    content: ' ';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40px;
    background-color: white;
    border-radius: 100%;
    transform: translateY(50%);
  }
  .login-icon {}
  .login-btn {
    width: 90%;
    background-color: #C00000;
    border-radius: 150px;
    margin: 10px 0;
  }
  .login-tips {
    font-size: 12px;
    color: gray;
  }
}
</style>
